﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using MLSMooc.BO;
using MLSMooc.DAO.Implement.Project;
using MLSMooc.Helper;
using MLSMooc.Service.Implement.Project;

namespace MLSMooc.Controllers
{
    public class ProjectController : Controller
    {
        private static ProjectDAO ProjectDAO =ProjectDAO.Getinstance();
        private static ProjectServiceImpl projectService =ProjectServiceImpl.Getinstance();

        [HttpGet]
        public async Task<string> GetByIdProjectInfo(int id)
        {
            var res = await ProjectDAO.GetByIdAsync(id); 
            return SerializeHelper.SerializeJson(res);
        }

        [HttpGet]
        public async Task<string> GetProjectList()
        {
            var res = await ProjectDAO.GetListAsync();
            return SerializeHelper.SerializeJson(res);
        }

        [HttpGet]
        public async Task<string> GetProjectVideoList()
        {
            var res = await ProjectDAO.GetListVideoAsync();
            return SerializeHelper.SerializeJson(res);
        }

        [HttpGet]
        public async Task<string> GetTeachAsync(string id)
        {
            var res = await ProjectDAO.GetTeachAsync(id);
            return SerializeHelper.SerializeJson(res);
        }

        [HttpPost]
        public async Task<int> InsertProBrowseInfo(BrowseBO data)
        {
            var res = await projectService.InsertProBrowseInfo(data);
            return res;
        }
        //测试题-上传测试题配图
        [HttpPost]
        public ActionResult uploadExamImg()
        {
            try
            {
                var file = Request.Files[0];
                var filecombin = file.FileName.Split('.');
                if (file == null || String.IsNullOrEmpty(file.FileName) || file.ContentLength == 0 || filecombin.Length < 2)
                {
                    return Json(new
                    {
                        fileid = 0,
                        src = "",
                        name = "",
                        msg = "上传出错 请检查文件名 或 文件内容"
                    });
                }
                //定义本地路径位置
                string local = "E:/websites/MLSLab/MLSLabDome/img/Mooc_Examimg\\" + System.DateTime.Now.ToString("yyyy-MM-dd");
                //string local = "C:/Users/DELL/Desktop/慕课+医学系3D项目/img/Mooc_Examimg\\" + System.DateTime.Now.ToString("yyyy-MM-dd");
                //string filePathName = string.Empty;
                var filePathName = file.FileName.Insert(file.FileName.LastIndexOf('.'), "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff"));
                string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, local);

                //var tmpName = Server.MapPath("~/Upload/" + System.DateTime.Now.ToString("yyyy-MM-dd") + "/");
                //var tmp = file.FileName;
                //var tmpIndex = 0;
                ////判断是否存在相同文件名的文件 相同累加1继续判断
                //while (System.IO.File.Exists(tmpName + tmp))
                //{
                //    return Json(new
                //    {
                //        src = "",
                //        name = "",   // 获取文件名不含后缀名
                //        msg = "文件名存在请重新上传",
                //        fileName = ""
                //    });
                //    //tmp = filecombin[0] + "_" + ++tmpIndex + "." + filecombin[1];
                //}

                ////不带路径的最终文件名
                //filePathName = tmp;

                if (!System.IO.Directory.Exists(localPath))
                    System.IO.Directory.CreateDirectory(localPath);
                string localURL = Path.Combine(local, filePathName);
                file.SaveAs(Path.Combine(localPath, filePathName));   //保存图片（文件夹）
                return Json(new
                {
                    src = localURL.Trim().Replace("\\", "/"),
                    name = Path.GetFileNameWithoutExtension(file.FileName),   // 获取文件名不含后缀名
                    msg = "上传成功",
                    fileName = System.DateTime.Now.ToString("yyyy-MM-dd") + "/" + filePathName//最终返回的路径及文件名称

                });
            }
            catch { }
            return Json(new
            {
                src = "",
                name = "",   // 获取文件名不含后缀名
                msg = "上传出错",
                fileName = ""
            });

        }
    }
}